package com.hengfoxwolf.jnidemo
import android.os.Bundle
import android.util.Log
import com.hengfoxwolf.jnidemo.bean.LearnThing
import com.hengfoxwolf.jnidemo.databinding.ActivityMainBinding

class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {

    override fun init(savedInstanceState: Bundle?) {

        mBinding.sampleText.text = stringFromJNI()

        Log.d(TAG, "defaultValue:${intFromJNI()}")

        Log.d(TAG, "calculateValue:${calculateFromJNI(10, 5, 3)}")

        val dataSet = mutableListOf(1, 2, 3, 4, 5, 6, 7)
        val newDataSet = filterFromJNI(dataSet)
        for (value in newDataSet) {
            Log.d(TAG, "new--value:${value}")
        }


        val map = mutableMapOf<String, String>().apply {
            put("1","one")
            put("2","two")
            put("3","three")
            put("4","four")
        }

        val newMap = filterMapFromJNI(map)
        Log.d(TAG, "size:${newMap.size}")


        //数组
        val sourceArray = IntArray(6)
        for (i in sourceArray.indices) {
            sourceArray[i] = i * i
        }
        val resultArray = filterArrayFromJNI(sourceArray = sourceArray)
        for (value in resultArray) {
            Log.d(TAG, "value--${value}")
        }


        val strArray = arrayOf("a", "b", "c", "d")
        val intArray = filterArrayObjectFromJNI(strArray)
        for (item in intArray) {
            Log.d(TAG, "item--${item}")
        }

        val l0Arr = arrayOf(
            LearnThing(10,"10"),
            LearnThing(20,"20"),
            LearnThing(30,"30"),
            LearnThing(40,"40"),
        )

        val l1Arr = filterArrayDefineObjectFromJNI(l0Arr)
        for (item in l1Arr) {
            Log.d(TAG, "data:${item.data},content:${item.content}")
        }

        val l0List = mutableListOf(
            LearnThing(110,"110"),
            LearnThing(220,"220"),
            LearnThing(330,"330"),
            LearnThing(440,"440")
        )

        val l1List: MutableList<LearnThing> = filterListDefineObjectFromJNI(l0List)
        Log.d(TAG, "data--size:${l1List.size}")
        for (item in l1List) {
            Log.d(TAG, "data00:${item.data},content00:${item.content}")
        }


        val d0Map = mutableMapOf<Int, LearnThing>().apply {
            put(1,LearnThing(510,"510"))
            put(2,LearnThing(610,"610"))
            put(3,LearnThing(710,"710"))
            put(4, LearnThing(810, "810"))
        }
        val d1Map = filterMapDefineObjectFromJNI(d0Map)
        for ((key, value) in d1Map) {
            Log.d(TAG,"key:${key},value--data:${value.data},${value.content}")
        }

        Log.d(TAG, "value:${stringFromJNIInOtherLib()}")

        Log.d(TAG, "value000:${stringFromJNIInSo("124", "456")}")


        Log.d(TAG, "dynamic:${dynamicRegisterMethod()}")

        val operateStr = operateStringFromJNI("test")
        Log.d(TAG, "operateStr:${operateStr}")
    }

    fun doTest() {
        Log.d(TAG, "pioddasdfasfasfva0108")
    }

    /**
     * A native method that is implemented by the 'jnidemo' native library,
     * which is packaged with this application.
     */
    external fun stringFromJNI(): String

    external fun intFromJNI(): Int

    external fun calculateFromJNI(a: Int, b: Int, flag: Int): Int

    external fun filterFromJNI(dataSet:MutableList<Int>): MutableList<Int>

    external fun filterMapFromJNI(map: MutableMap<String, String>): MutableMap<Float, Float>

    external fun filterArrayFromJNI(sourceArray: IntArray): IntArray

    external fun filterArrayObjectFromJNI(sourceArray: Array<String>): Array<Int>

    external fun filterArrayDefineObjectFromJNI(sourceArray: Array<LearnThing>): Array<LearnThing>

    external fun filterListDefineObjectFromJNI(sourceArray: MutableList<LearnThing>): MutableList<LearnThing>

    external fun filterMapDefineObjectFromJNI(sourceArray: MutableMap<Int,LearnThing>): MutableMap<Int,LearnThing>

    private external fun stringFromJNIInOtherLib(): String

    //动态注册JNI方法
    private external fun dynamicRegisterMethod(): String

    private external fun operateStringFromJNI(value: String): String


    private external fun stringFromJNIInSo(value0:String,value1: String): String

    companion object {
        // Used to load the 'jnidemo' library on application startup.
        init {
            System.loadLibrary("jnidemo")
            System.loadLibrary("otherlib")
        }

        const val TAG = "MainActivity"
    }
}